package com.example.a22310.myapplication.Abstractclass;

//红色的R解决不了
import com.example.a22310.myapplication.R;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.view.Menu;
import android.view.MenuItem;

import com.example.a22310.myapplication.Activity.MainActivity;
import com.example.a22310.myapplication.Beans.user;
import com.example.a22310.myapplication.Listener.ClubListener;
import com.example.a22310.myapplication.Model.UserModel;
import com.example.a22310.myapplication.Service.service;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

import static android.provider.ContactsContract.CommonDataKinds.Website.URL;
import static com.example.a22310.myapplication.GloableClass.Yuejian.NEWS_NOTIFICATION;

public class MyService extends Service implements ClubListener<user> {
    private MyThread myThread;
    private int userid = 1;
    private String FILENAME = "yuejian'data";
    private String newsNumber;
    int a = 0;


    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onStart(Intent intent, int startId) {
        myThread = new MyThread();
        myThread.start();
        super.onStart(intent, startId);
    }

    @Override
    public void onDestroy() {
        NEWS_NOTIFICATION = false;
        super.onDestroy();
    }

    @Override
    public void setContentView() {

    }

    @Override
    public void initView() {

    }

    @Override
    public void initEvent() {

    }

    @Override
    public void setTitle() {

    }

    @Override
    public void onBackPressed() {

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        return false;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        return false;
    }

    @Override
    public void onSuccess(user list) {
        newsNumber = list.getNewsBubbleNumber();
        if (!newsNumber.equals("0")) {

//                Intent intent = new Intent("refresh_user_news");
//                intent.putExtra("newsNum",newsNumber);
//                sendBroadcast(intent);
            //               newsNotification();


        }
    }

    @Override
    public void onFailure() {

    }

    class MyThread extends Thread {
        @Override
        public void run() {
            while (NEWS_NOTIFICATION) {
                //  newsaa();
                //readNews();

            }
            try {
                Thread.sleep(2000);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public void readNews() {  //获取消息数

        UserModel userModel = new UserModel();
        userModel.newsNumber(this, 1);
    }

//    private int  readID(){  //获取用户id
//        int newsNumber;
//        SharedPreferences sharedPreferences = getSharedPreferences(FILENAME, Context.MODE_PRIVATE);
//        newsNumber = sharedPreferences.getInt("userId",0);
//        return newsNumber;
//    }

    private void newsNotification() {

        NotificationManager notificationmanager = (NotificationManager) getApplication().getSystemService(Context.NOTIFICATION_SERVICE);
        //  Notification notification = new Notification(R.drawable.ic_coupon,tistr, System.currentTimeMillis());

        //  notification.setLatestEventInfo(context,msgstr,null,pintent);

        Notification.Builder builder = new Notification.Builder(getApplication());//新建Notification.Builder对象
        Intent notificationIntent = new Intent(getApplication(), MainActivity.class);
        // PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
        PendingIntent pintent = PendingIntent.getActivity(getApplication(), 0, new Intent(getApplication(), MainActivity.class), 0);

        builder.setContentTitle("约见");//设置标题
        builder.setContentText("你有新的消息");//设置内容
        builder.setSmallIcon(R.drwable.yuejianlog);//设置图片R.drawable.yuejianlog
        builder.setContentIntent(pintent);//执行intent
        builder.setAutoCancel(true);
        Notification notification = builder.getNotification();//将builder对象转换为普通的notification
        // startForeground(1,notification);//让 MyService 变成一个前台服务，并在系统状态栏中显示出来
        notification.defaults |= Notification.DEFAULT_SOUND;
        //notification.defaults |= Notification.DEFAULT_VIBRATE;
        long[] vibrate = {0, 100, 200, 300};
        notification.vibrate = vibrate;
        //  notification.defaults |=Notification.DEFAULT_LIGHTS;
        notification.ledARGB = 0x00ff00;
        notification.ledOnMS = 300;
        notification.ledOffMS = 1000;
        notification.flags |= Notification.FLAG_SHOW_LIGHTS;

        notificationmanager.notify(1, notification);
    }


    private void newsaa() {
        Retrofit retrofit = new Retrofit.Builder()
                .addConverterFactory(GsonConverterFactory.create())
                .baseUrl(URL)
                .build();
        service service = retrofit.create(service.class);
        Call<user> call = service.newsBubbleNumber(1);
        //3
        call.enqueue(new Callback<user>() {
            @Override
            public void onResponse(Call<user> arg0, Response<user> arg1) {
                // TODO Auto-generated method stub
                user user = arg1.body();
                if (!user.getNewsBubbleNumber().equals("0")) {

                    if (a != Integer.parseInt(user.getNewsBubbleNumber())) {
                        newsNotification();
                    }
                    a = Integer.parseInt(user.getNewsBubbleNumber());

                }

            }

            @Override
            public void onFailure(Call<user> arg0, Throwable arg1) {
                // TODO Auto-generated method stub
                //Toast

            }
        });
    }
}